package ncku.account.unitTest;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import ncku.account.data.Category;
import ncku.account.data.GlobalData;
import ncku.account.data.SubTransaction;
import ncku.account.data.SubTransactionList;
import ncku.account.data.Transaction;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class GlobalDataTest {
	private GlobalData globalData;
	
	//for input Date
	
	public SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
	//public DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
	
	//add transaction variables
	private String inputDate;
	private int inputAmount;
	private String inputDescription;
	
	private int inputCateSel;
	private Category inputCategory;
	
	private ArrayList<SubTransaction> inputSubTrans;
	private SubTransactionList inputSubTransList;
	private int SubTransAmount;
	private String SubTransDescription;
	
	@Before
    public void setUp(){
    	globalData = new GlobalData();
    	inputSubTransList = new SubTransactionList();
    	globalData.balanceMoney = 0;
    }
    @After
    public void tearDown(){
    	globalData = null;
    }
    @Test
    public void test(){
    	CategoryTest();
    	TransactionTest();
    }
    
    public void CategoryTest(){
    	//add Category Name
    	String Cate1 = "SSSSSSSSSSSSSSSSSSSalary";
    	String Cate2 = "Eat";
    	String Cate3 = "Travel";
    	String Cate4 = "書籍";
    	String newCate = "薪水";
    	
    	Category receiver;
    	int categorySize;
    	
    	String expected;
    	int expectedNum;
    	
    	//add category test
    	globalData.add(Cate1);
    	globalData.add(Cate2);
    	globalData.add(Cate3);
    	globalData.add(Cate4);
    	
    	receiver = globalData.getCategory(0);
    	expected = Cate1;
    	assertEquals(expected,receiver.getName());
    	receiver = globalData.getCategory(1);
    	expected = Cate2;
    	assertEquals(expected,receiver.getName());
    	receiver = globalData.getCategory(2);
    	expected = Cate3;
    	assertEquals(expected,receiver.getName());
    	receiver = globalData.getCategory(3);
    	expected = Cate4;
    	assertEquals(expected,receiver.getName());
    	
    	categorySize = globalData.getCategoryNumber();
    	expectedNum = 4;
    	assertEquals(expectedNum,categorySize);
    	
    	//edit category test
    	globalData.edit(newCate, 0);
    	receiver = globalData.getCategory(0);
    	expected = newCate;
    	assertEquals(expected,receiver.getName());
    	
    	//delete category test
    	globalData.deleteCategory(2);
    	receiver = globalData.getCategory(2);
    	expected = Cate4;
    	assertEquals(expected,receiver.getName());
    	categorySize = globalData.getCategoryNumber();
    	expectedNum = 3;
    	assertEquals(expectedNum,categorySize);
    }
    
    public void TransactionTest(){
    	Transaction receiver;
    	int balanceMoney;
    	String expectedString;
    	int expectedInt;
    	
    	//要新增的資料(1)
    	inputDate = "2013/1/14";
    	Calendar calendar = Calendar.getInstance();
    	//calendar.setTime(sdf.parse(inputDate));
    	
    	//Date date = sdf.parse(inputDate);
    	Date date = calendar.getTime();
    	
    	inputAmount = 100;
    	inputCateSel = 0;
    	inputDescription = "1月份薪水";
  
    	inputCategory = globalData.getCategory(inputCateSel);
    	inputSubTrans = inputSubTransList.getArrayListSubTrans();
    	
    	globalData.add(date, inputDescription, inputAmount, inputCategory, inputSubTrans); // add transaction
    
    	//add trnasaction test(1)
    	receiver = globalData.getTransaction(0);
    //	expectedString = receiver.getDate();
    //	assertEquals(expectedString,inputDate);
    	expectedInt = receiver.getAmount();
    	assertEquals(expectedInt,inputAmount);
    	expectedString = receiver.getCategory();
    	assertEquals(expectedString,globalData.getCategory(inputCateSel).getName());
    	expectedString = receiver.getDescription();
    	assertEquals(expectedString,inputDescription);
    	
    	expectedInt = 100;
    	balanceMoney = globalData.balanceMoney;
    	assertEquals(expectedInt,balanceMoney);
    	
    	//edit transaction
    	inputAmount = 500;
    	inputDescription = "1月份薪水+分紅";
    	globalData.edit(0, date, inputDescription, inputAmount, inputCategory, inputSubTrans);
    	
    	//edit transaction testing
    	receiver = globalData.getTransaction(0);
    	expectedInt = receiver.getAmount();
    	assertEquals(expectedInt,inputAmount);
    	expectedString = receiver.getDescription();
    	assertEquals(expectedString,inputDescription);
    	
    	expectedInt = 500;
    	balanceMoney = globalData.balanceMoney;
    	assertEquals(expectedInt,balanceMoney);
    	
    	
    	//要新增的資料(2)
    	inputAmount = -150;
    	inputCateSel = 1;
    	inputDescription = "晚餐";
 
    	inputCategory = globalData.getCategory(inputCateSel);
    	
    	//new SubTransaction and testing
    	SubTransAmount = -80;
    	SubTransDescription = "海鮮燴飯";
    	inputSubTransList.addSubTrans(SubTransDescription, SubTransAmount);
    	expectedString = inputSubTransList.getSubTrans(0).getSubDescription();
    	assertEquals(expectedString,SubTransDescription);
    	expectedInt = inputSubTransList.getSubTrans(0).getSubAmount();
    	assertEquals(expectedInt,SubTransAmount);
    	
    	SubTransAmount = -25;	
    	SubTransDescription = "蛋花湯";
    	inputSubTransList.addSubTrans(SubTransDescription, SubTransAmount);
    	expectedString = inputSubTransList.getSubTrans(1).getSubDescription();
    	assertEquals(expectedString,SubTransDescription);
    	expectedInt = inputSubTransList.getSubTrans(1).getSubAmount();
    	assertEquals(expectedInt,SubTransAmount);
    	
    	SubTransAmount = -45;
    	SubTransDescription = "西點小餅乾";
    	inputSubTransList.addSubTrans(SubTransDescription, SubTransAmount);
    	expectedString = inputSubTransList.getSubTrans(2).getSubDescription();
    	assertEquals(expectedString,SubTransDescription);
    	expectedInt = inputSubTransList.getSubTrans(2).getSubAmount();
    	assertEquals(expectedInt,SubTransAmount);
    	
    	inputSubTrans = inputSubTransList.getArrayListSubTrans();

    	globalData.add(date, inputDescription, inputAmount, inputCategory, inputSubTrans); // add transaction
    	receiver = globalData.getTransaction(1);
    //	expectedString = receiver.getDate();
    //	assertEquals(expectedString,inputDate);
    	expectedInt = receiver.getAmount();
    	assertEquals(expectedInt,inputAmount);
    	expectedString = receiver.getCategory();
    	assertEquals(expectedString,globalData.getCategory(inputCateSel).getName());
    	expectedString = receiver.getDescription();
    	assertEquals(expectedString,inputDescription);
    	
    	expectedInt = 350;
    	balanceMoney = globalData.balanceMoney;
    	assertEquals(expectedInt,balanceMoney);
    	
    	//check subtransactions
    	expectedString = receiver.getSubTrans(0).getSubDescription();
    	assertEquals(expectedString,"海鮮燴飯");
    	expectedInt = receiver.getSubTrans(0).getSubAmount();
    	assertEquals(expectedInt,-80);
    	expectedString = receiver.getSubTrans(1).getSubDescription();
    	assertEquals(expectedString,"蛋花湯");
    	expectedInt = receiver.getSubTrans(1).getSubAmount();
    	assertEquals(expectedInt,-25);
    	expectedString = receiver.getSubTrans(2).getSubDescription();
    	assertEquals(expectedString,"西點小餅乾");
    	expectedInt = receiver.getSubTrans(2).getSubAmount();
    	assertEquals(expectedInt,-45);
    	
    	//get transaction size
    	expectedInt = 2;
    	assertEquals(expectedInt,globalData.getTransactionNumber());
    	
    	//delete transaction(1)
    	globalData.delete(1);
    	expectedInt = 500;
    	balanceMoney = globalData.balanceMoney;
    	assertEquals(expectedInt,balanceMoney);
    	
    	//delete transaction(2)
    	globalData.delete(0);
    	expectedInt = 0;
    	balanceMoney = globalData.balanceMoney;
    	assertEquals(expectedInt,balanceMoney);

    }
}
